﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;


namespace covua
{
    public class DATABASECONNECT
    {
        public OleDbConnection connectSYSTEM;
        public OleDbCommand Command;
        public OleDbDataReader ReadCommand;
        public void CreateConnect()//string datasource)
        {
            connectSYSTEM = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Application.StartupPath + @"\db1.mdb;Jet OLEDB:Database Password=MyDbPassword;");
            try
            {
                connectSYSTEM.Open();
              //  MessageBox.Show("Ket noi Du Lieu Thanh cong");
            }
            catch
            {

                MessageBox.Show("Khong ket noi dc du lieu");

            }
        }
        public void ReadDataBase(string ReadStr)
        {
           
            Command = new OleDbCommand(ReadStr, connectSYSTEM);
            try
            {
                ReadCommand = Command.ExecuteReader();
            }
            catch
            {
                MessageBox.Show("Ko doc duoc du lieu");
            }
        }
        public void UpdateDataBase()
        {
        }

        public void Insert(string str)//Them  vao
        {
            Command = new OleDbCommand(str, connectSYSTEM);

            OleDbCommand commandThem = new OleDbCommand(str, connectSYSTEM);
            commandThem.CommandType = CommandType.Text;
            if (commandThem.ExecuteNonQuery() > 0)
            { 
            }
                //MessageBox.Show("Bạn đã đăng ký thành công", "Thông báo");
        //    Command.ExecuteNonQuery();
        }

        public void Update(string str)//Them  vao
        {
           // CreateConnect();
            Command = new OleDbCommand(str, connectSYSTEM);

            OleDbCommand commandUp = new OleDbCommand(str, connectSYSTEM);
            commandUp.CommandType = CommandType.Text;
         //   if (commandUp.ExecuteNonQuery() > 0)
                MessageBox.Show("Update thanh cong", "Thông báo");
            //Command.ExecuteNonQuery();
        }

        public void Delete(string str)//Them  vao
        {
            Command = new OleDbCommand(str, connectSYSTEM);

            OleDbCommand commandXoa =  new OleDbCommand(str, connectSYSTEM);
            commandXoa.CommandType = CommandType.Text;
           // if (commandXoa.ExecuteNonQuery() > 0)
                MessageBox.Show("Thành công!", "Thông báo");
           
             
            //Command.ExecuteNonQuery();
        }

        public void CloseConnect()
        {
            connectSYSTEM.Close();
        }
    }

    public class TAIKHOAN
    {
        public string Username;
        public bool KiemTra(string user, string pass, bool IsSignin, DATABASECONNECT data)
        {
           // data.CreateConnect();
            data.ReadDataBase("select * from NguoiChoi");
            string str1,str2;

            while (data.ReadCommand.Read())
            {
                if (IsSignin == true)//Nếu là đăgn nhập
                    if ((data.ReadCommand.GetString(0).ToString() == user) && (data.ReadCommand.GetString(1).ToString() == pass))
                    {//nếu đăng nhập đúng

                        Username = user;
                        str1 = data.ReadCommand.GetString(0).ToString();
                       // MessageBox.Show(str1);
                        str2 = data.ReadCommand.GetString(1).ToString();
                       // MessageBox.Show(str2);
                        return true;
                    }
                   /* else//Nếu là đăng ký
                        if (data.ReadCommand.GetString(0) == user)//nếu đăng ký bị trùng
                        {
                            return true;
                        }
                    */
            }

            if (IsSignin == false)//Nếu đăng ký thành công
                Username = user;
            return false;
        }

        public bool KiemTraDK(string user, string pass, bool IsSignin, DATABASECONNECT data)
        {
            // data.CreateConnect();
            data.ReadDataBase("select * from NguoiChoi");
            
            while (data.ReadCommand.Read())
            {
                if (IsSignin == false)
                    if ((data.ReadCommand.GetString(0).ToString() == user))
                    {
                        Username = user;
                        return true;
                    }
                /* else//Nếu là đăng ký
                     if (data.ReadCommand.GetString(0) == user)//nếu đăng ký bị trùng
                     {
                         return true;
                     }
                 */
            }
            return false;
        }

        public bool DangNhap(string user, string pass, DATABASECONNECT data)
        {
            return KiemTra(user, pass, true, data);
        }
        public bool DangKy(string user, string pass, DATABASECONNECT data)
        {
            if (KiemTraDK(user, pass, false, data))//Neu tai khoang trùng  
            {
                MessageBox.Show("Tên đăng ký bị trùng", "Thông báo");
                return false;
            }
            else
            {  
                string strSql = "Insert into NguoiChoi(Ten, Pass, SoVanThang, SoVanThua, DiemSo) values ('" + user + "','" + pass + "','0','0','0')";

                //connectSYSTEM = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Application.StartupPath + @"\db1.mdb;Jet OLEDB:Database Password=MyDbPassword;");
                //connectSYSTEM.Open();
                

                data.Insert(strSql);
              //  MessageBox.Show("Bạn đã đăng ký thành công", "Thông báo");
                return true;
            }
        }
        public void DangXuat(DATABASECONNECT data)
        {
            data.CloseConnect();
        }

        public void Update_Thang(string user, DATABASECONNECT data)
        { 
            data.ReadDataBase("select * from NguoiChoi");

            while (data.ReadCommand.Read())
            {
                if ((data.ReadCommand.GetString(0).ToString() == user))
                {
                    string pass, SoThang, SoThua, Diem;
                    pass = data.ReadCommand.GetString(1).ToString();
                    SoThang = data.ReadCommand.GetString(2).ToString();
                    SoThua = data.ReadCommand.GetString(3).ToString();
                    int x, y, z;
                    x = int.Parse(SoThang);
                    y = int.Parse(SoThua);
                    x = x + 1;
                    z = x - y;
                    SoThang = x.ToString();
                    SoThua = y.ToString();
                    Diem = z.ToString();
                   
                    string sql = "update NguoiChoi set Pass='" + pass + "',SoVanThang='" + SoThang + "',SoVanThua='" + SoThua + "',DiemSo='" + Diem +"' where Ten ='"+ user +"'";
                   // >	covua.exe!covua.DATABASECONNECT.Insert(string str = "update NguoiChoi set Pass='123',SoVanThang='1',SoVanThua='0',DiemSo='1', where Ten ='hung'") Line 54	C#              
                    data.Insert(sql);
                }
            }
        }
        public void Update_Thua(string user, DATABASECONNECT data)
        {
            data.ReadDataBase("select * from NguoiChoi");

            while (data.ReadCommand.Read())
            {
                if ((data.ReadCommand.GetString(0).ToString() == user))
                {
                    string pass, SoThang, SoThua, Diem;
                    pass = data.ReadCommand.GetString(1).ToString();
                    SoThang = data.ReadCommand.GetString(2).ToString();
                    SoThua = data.ReadCommand.GetString(3).ToString();
                    int x, y, z;
                    x = int.Parse(SoThang);
                    y = int.Parse(SoThua);
                    y = y + 1;
                    z = x - y;
                    SoThang = x.ToString();
                    SoThua = y.ToString();
                    Diem = z.ToString();

                    string sql = "update NguoiChoi set Pass='" + pass + "',SoVanThang='" + SoThang + "',SoVanThua='" + SoThua + "',DiemSo='" + Diem + "' where Ten ='" + user + "'";
                    // >	covua.exe!covua.DATABASECONNECT.Insert(string str = "update NguoiChoi set Pass='123',SoVanThang='1',SoVanThua='0',DiemSo='1', where Ten ='hung'") Line 54	C#              
                    data.Insert(sql);
                }
            }
        }

        public String GetUsername()
        {
            return Username;
        }

        public void ThongTin(string user, string pass, DATABASECONNECT data)
        {
            // data.CreateConnect();
            data.ReadDataBase("select * from NguoiChoi");
            string str1,str2,str3,str4,str5;
            int x = 0;
            while (data.ReadCommand.Read())
            {
             
                    if ((data.ReadCommand.GetString(0).ToString() == user) && (data.ReadCommand.GetString(1).ToString() == pass))
                    {
                        x = 1;
                        Username = user;
                        str1 = data.ReadCommand.GetString(0).ToString();
                        // MessageBox.Show(str1);
                        str2 = data.ReadCommand.GetString(1).ToString();
                        str3 = data.ReadCommand.GetString(2).ToString();
                        str4 = data.ReadCommand.GetString(3).ToString();
                        str5 = data.ReadCommand.GetString(4).ToString();
                        MessageBox.Show("Tên Đăng Nhập: "+str1+"\n Số Ván Thắng: "+str3+"\n Số Ván Thua: "+str4+"\n Điểm Số :"+str5,"Thông Tin Tài Khoản");
                        // MessageBox.Show(str2);
                       
                    }
               
            }
            if (x == 0)
                MessageBox.Show("Tên Đăng Nhập  hoặc Mật Khẩu khônng chính xác", "Thông báo");
          
        }

    }
    static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
